package com.danskin.wechat.webapi;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.danskin.wechat.vo.CustCouponApiView;
import com.danskin.wechat.constant.DanskinConstants;
import com.danskin.wechat.entity.CustCouponRecord;
import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.service.CouponService;
import com.danskin.wechat.util.ConvertUtils;
import com.danskin.wechat.util.Page;

@RestController
@RequestMapping("api/coupon")
public class CustCouponController extends BaseApiController {

	@Autowired
	public CouponService couponService;

	// @RequestMapping(value = "json", method = RequestMethod.POST, produces =
	// MediaType.APPLICATION_JSON_UTF8_VALUE)
	// public Object test(@RequestBody List<CustCouponRequest> list) {
	// String sql = "";
	// for (CustCouponRequest r : list) {
	// sql += ("insert into sys_area(code,name,parent_code) values('" +
	// r.getCode() + "','"
	// + r.getName() + "','" + r.getParentCode() + "'); ");
	// }
	// System.out.println(sql);
	// return sql;
	// }

	/**
	 * 获取会员优惠券列表
	 * 
	 * @param map
	 * @param appId
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getCustCoupon/{appId}")
	public Object getCustCoupon(@RequestBody Map<String, Object> map, @PathVariable("appId") String appId,
			HttpServletRequest request) throws Exception {

		if (false == this.isValid("type", map)) {
			return this.getError("type不能为空");
		} else if (false == this.isValid("pageIndex", map)) {
			return this.getError("pageIndex不能为空");
		} else if (false == this.isValid("openId", map)) {
			return this.getError("openId不能为空");
		}

		String sType = map.get("type").toString();
		String sPageIndex = map.get("pageIndex").toString();

		int pageIndex = ConvertUtils.getInt(sPageIndex);
		int pageSize = 10;
		map.put("appId", appId);
		map.put("mpId", this.getMpId(appId));
		Page page = new Page();
		page.setCurrentResult(pageIndex);
		page.setShowCount(pageSize);
		page.setMap(map);

		List<CustCouponApiView> list = couponService.listPageByOpenId(page, sType);
		for (CustCouponApiView view : list) {
			if (view.getType() != null && view.getType().equals(DanskinConstants.WX_CARD_CONFIG_CARD_TYPE_DISCOUNT)) {
				BigDecimal discount = new BigDecimal(view.getTnum()).multiply(new BigDecimal(10));
				view.setTnum(discount.toString());
			}
		}
		return new AccessResponse<Object>("0", "ok", list);
	}

	/**
	 * 优惠券详情
	 * 
	 * @param map
	 * @param appId
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "getCustCouponDetail/{appId}", method = RequestMethod.POST)
	public Object getCustCouponDetail(@RequestBody Map<String, Object> map, @PathVariable("appId") String appId,
			HttpServletRequest request) throws Exception {

		if (false == this.isValid("couponId", map)) {
			return this.getError("couponId不能为空");
		}
		String sCouponId = map.get("couponId").toString();

		int couponId = ConvertUtils.getInt(sCouponId);

		CustCouponApiView view = couponService.findCustCouponView(couponId, appId);
		if (view != null && view.getType().equals(DanskinConstants.WX_CARD_CONFIG_CARD_TYPE_DISCOUNT)) {
			BigDecimal discount = new BigDecimal(view.getTnum()).multiply(new BigDecimal(10));
			view.setTnum(discount.toString());
		}

		return new AccessResponse<Object>("0", "ok", view);
	}

	@RequestMapping(value = "isGetWx/{appId}", method = RequestMethod.POST)
	public Object isGetWx(@RequestBody Map<String, Object> map, @PathVariable("appId") String appId,
			HttpServletRequest request) throws Exception {

		if (!map.containsKey("couponId")) {
			return this.getError("couponId不能为空");
		}
		if (!map.containsKey("couponNo")) {
			return this.getError("couponNo不能为空");
		}
		String sCouponId = map.get("couponId").toString();
		String couponNo = map.get("couponNo").toString();

		int couponId = ConvertUtils.getInt(sCouponId);

		CustCouponRecord record = couponService.findCustCoupon(couponId, couponNo);
		if (record == null) {
			return this.getError("该券无效，请核实有效性。");
		}
		JSONObject json = new JSONObject();
		json.put("isgetwx", record.getIsWxGet());
		return new AccessResponse<Object>("0", "ok", json);
	}

}
